package com.tzbank.face.esb.provide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.dc.eai.data.CompositeData;
import com.dcfs.esb.server.service.Service;
import com.tzbank.esb.util.EsbConversionUtil;
import com.tzbank.face.entity.FaceModel;
import com.tzbank.face.entity.Metainfo;
import com.tzbank.face.service.FaceService;
import com.tzbank.face.utils.GeneralSeqNo;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.SpringContextUtils;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
@Slf4j
public class EsbIntoFaceServiceImpl implements Service {

	static FaceService faceService = SpringContextUtils.getBeanByName("faceService");

	/**
	 * 该类是发布在ESB在人脸服务的实现类 获取报文参数然后执行业务代码，之后再组装报文返回 详情如下：
	 * 
	 * @param data
	 * 
	 * @return 响应报文
	 */

	public CompositeData exec(CompositeData data) {
		log.info("==========进入Face_BIS服务========");
		CompositeData result = new CompositeData();
		// 系统头
		String serviceCode = (String) data.getStruct("SYS_HEAD").getField("SERVICE_CODE").getValue();// 服务码
		String CONSUMER_SEQ_NO = (String) data.getStruct("SYS_HEAD").getField("CONSUMER_SEQ_NO").getValue();// 系统流水
		String SERVICE_SCENE = (String) data.getStruct("SYS_HEAD").getField("SERVICE_SCENE").getValue();

		String SeqNo = GeneralSeqNo.generalGlobalSeqNo("FRS");//服务流水
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> publicMap = new HashMap<String, Object>();
		publicMap.put("CONSUMER_SEQ_NO", CONSUMER_SEQ_NO);// 是服务请求系统的流水号
		publicMap.put("ESB_SEQ_NO", "");// （ESB产生的流水号）为ESB系统的流水号

		publicMap.put("BUSS_SEQ_NO", "");//// 业务流水号）为应用头请求中BUSS_SEQ_NO
		publicMap.put("SERV_SEQ_NO", SeqNo);// 服务生成流水

		if ("02".equals(SERVICE_SCENE)) {// 人脸识别服务

			String certName = (String) data.getStruct("BODY").getField("CLIENT_NAME").getValue();// 身份证号
			String certNo = (String) data.getStruct("BODY").getField("GLOBAL_ID").getValue();//
			String requestSource = (String) data.getStruct("BODY").getField("SOURCE_CHANEL").getValue();// 请求来源
			String userId = (String) data.getStruct("BODY").getField("USER_NO").getValue();// 请求来源
			String metainfo = (String) data.getStruct("BODY").getField("DEVICE_INFO").getValue();// 请求来源

			if (StringUtils.isEmpty(metainfo) || StringUtils.isEmpty(certNo) || StringUtils.isEmpty(certName)
					|| StringUtils.isEmpty(requestSource) || StringUtils.isEmpty(userId)) {

				result = toChang(new HashMap<String, Object>(), publicMap);
				log.info("外部系统调用参数为空");
			} else {

				FaceModel vo = new FaceModel();
				JSONObject jsonObject = JSONObject.fromObject(metainfo);
				Metainfo meifo = (Metainfo) jsonObject.toBean(jsonObject, Metainfo.class);
				vo.setMetainfo(meifo);
				vo.setCertName(certName);
				vo.setCertNo(certNo);
				vo.setRequestSource(requestSource);
				vo.setUserId(userId);
				vo.setConsumerSeqNo(SeqNo);// 系统流水
				resultMap = faceService.intoFaceService(vo);
				result = toChang(resultMap, publicMap);

			}
		} else if ("03".equals(SERVICE_SCENE)) {// 查询服务
			String bizId = (String) data.getStruct("BODY").getField("DEMAND_ID").getValue();
			String zimId = (String) data.getStruct("BODY").getField("VERIFY_FLAG").getValue();
			String requestSource = (String) data.getStruct("BODY").getField("SOURCE_CHANEL").getValue();// 请求来源
			String userId = (String) data.getStruct("BODY").getField("USER_NO").getValue();// 请求来源

			if (StringUtils.isEmpty(bizId) || StringUtils.isEmpty(zimId)) {

				result = toChang(new HashMap<String, Object>(), publicMap);
				log.info("外部系统调用参数为空");
			} else {
				FaceModel vo = new FaceModel();
				vo.setRequestSource(requestSource);
				vo.setUserId(userId);
				vo.setBizId(bizId);
				vo.setZimId(zimId);
				vo.setConsumerSeqNo(SeqNo);// 系统流水
				resultMap = faceService.queryFaceService(vo);
				result = toChang(resultMap, publicMap);

			}
		} else {
			result = toChang(new HashMap<String, Object>(), publicMap);
		}
		log.info("外部系统调用結束Face_BIS>>服务流水"+SeqNo+"服务码："+SERVICE_SCENE);
		return result;
	}

	
	/**
	 * 将返回结果封装
	 * @param resultMap
	 * @param publicMap
	 * @return
	 */
	public CompositeData toChang(Map<String, Object> resultMap, Map<String, Object> publicMap) {

		CompositeData result = new CompositeData();
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = new HashMap<String, Object>();
		Map<String, Object> reqAppHead = new HashMap<String, Object>();
		List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>();
		Map<String, Object> retCode = new HashMap<String, Object>();
		reqSysHead.put("consumerSeqNo", publicMap.get("CONSUMER_SEQ_NO")); // 是服务请求系统的流水号
		reqSysHead.put("esbSeqNo", publicMap.get("ESB_SEQ_NO")); //（ESB产生的流水号）为ESB系统的流水号
		//应用头应答数据
		reqAppHead.put("bussSeqNo", publicMap.get("BUSS_SEQ_NO")); //业务流水号）为应用头请求中BUSS_SEQ_NO
		reqAppHead.put("servSeqNo", publicMap.get("SERV_SEQ_NO"));//服务流水
		inputMap.put("appHead", reqAppHead);
		if (resultMap.isEmpty()) {
			retCode.put("retMsg", "参数错误");
			retCode.put("retCode", "000000");
			ret.add(retCode);
			reqSysHead.put("ret", ret);
			reqSysHead.put("retStatus", "F");

		} else if(CodeUtil.CODE_200.getCode().equals(resultMap.get(resultMap.get("code")))){//结果成功获取
			//
			retCode.put("retMsg", "成功");
			retCode.put("retCode", CodeUtil.CODE_200.getCode());
			ret.add(retCode);
			reqSysHead.put("ret", ret);
			reqSysHead.put("retStatus", "S");

		}else {//程序异常
			//
			retCode.put("retMsg", resultMap.get("msg"));
			retCode.put("retCode", resultMap.get("code"));
			ret.add(retCode);
			reqSysHead.put("ret", ret);
			reqSysHead.put("retStatus", "F");

		}

		inputMap.put("sysHead", reqSysHead);
		inputMap.put("handleResult", resultMap.get("data"));// y
		// Map转换Esb报文
		result = EsbConversionUtil.mapToCompositeData(inputMap);
		return result;
	}

}
